<?php
//
defined('BY_JYA') or exit('error');
//cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$store_id=$this->session('store_id');
	if(!$store_id){
		exi('请先选择场馆！','error','jy_qiuyuyue/index/');
	}
	$carts=$this->session('carts');
	if(!$carts){
		exi('请先选择商品！','error');
	}
	$goodsprice = 0;
	if($carts){
		foreach($carts as $k=>&$v){
			$allnum += $v['num'];
			$goodinfo = pdo_get('jy_qiuyuyue_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$k));
			$v['packageinfo']=$goodinfo['package']?json_decode($goodinfo['package'],true):array();
			if(!empty($v['packageinfo'])){
				$goodprice = 0;
				foreach($v['packageinfo'] as $i=>&$p){
					$p['num']=$v['package'][$i];
					if(empty($p['price'])){
						$p['price']=$goodinfo['price'];
					}
					if(empty($p['num'])){
						unset($v['packageinfo'][$i]);
					}
					$goodsprice += $p['num']*$p['price'];
					$goodprice +=$p['num']*$p['price'];
				}	
			}else{
				$goodsprice += $v['num']*$goodinfo['price'];
				$goodprice =$v['num']*$goodinfo['price'];
			}
			$v['goodname']=$goodinfo['title'];
			$v['goodprice']=$goodprice;
			$v['goodnum']=$v['num'];
		}
	}
	$store=pdo_get('jy_qiuyuyue_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
	$youhui = 0;
	$allprice = $goodsprice- $youhui;;
	$return['carts']=$carts;
	$return['store']=$store;
	$return['allprice']=$allprice;
	$return['youhui']=$youhui;
	set_html('title','订单详情');
	$return['post']=array();
	exi($return);
}

	//提交订单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$store_id=$this->session('store_id');
	if(!$store_id){
		exi('请先选择场馆！','error','jy_qiuyuyue/index/');
	}	
	$store=pdo_get('jy_qiuyuyue_store',array('id'=>$store_id));
	$youhui = 0;
	$carts=$this->session('carts');
	if(!$carts){
		exi('没有选择商品！','error');
	}
	$total=0;
	foreach($carts as $k=>&$v){
			$allnum += $v['num'];
			$goodinfo = pdo_get('jy_qiuyuyue_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$k));
			$v['packageinfo']=$goodinfo['package']?json_decode($goodinfo['package'],true):array();
			if(!empty($v['packageinfo'])){
				foreach($v['packageinfo'] as $i=>&$p){
					$p['num']=$v['package'][$i];
					if(empty($p['price'])){
						$p['price']=$goodinfo['price'];
					}
					if(empty($p['num'])){
						unset($v['packageinfo'][$i]);
					}
					$total += $p['num']*$p['price'];
				}	
			}else{
				$total += $v['num']*$goodinfo['price'];
			}
	}
	$addressinfo=pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
	$memberinfo=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$price=$total - $youhui;
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>$store_id,
		'mid'=>$_SESSION['uid'],
		'ordersn'=>get_ordersn('jy_qiuyuyue_order','ordersn'),
		'price'=>$price,
		'createtime'=>TIMESTAMP		
	);
	pdo_insert('jy_qiuyuyue_order',$data);
	$order_id=pdo_insertid();	
	foreach($carts as $k=>&$v){
			$str='';
			$good=pdo_get('jy_qiuyuyue_goods',array('id'=>$k));
			$v['packageinfo']=$good['package']?json_decode($good['package'],true):array();
			if(!empty($v['packageinfo'])){
				foreach($v['packageinfo'] as $i=>&$p){
					$p['num']=$v['package'][$i];
					if(empty($p['price'])){
						$p['price']=$good['price'];
					}
					if(empty($p['num'])){
						unset($v['packageinfo'][$i]);
					}
				}
			}
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'store_id'=>$store_id,
				'order_id'=>$order_id,
				'goods_id'=>$k,
				'goods_num'=>$v['num'],
				'goods_price'=>$good['price'],			
				'goods_title'=>$good['title'],
				'goods_package'=>ijson_encode($v['packageinfo']),
			);
			pdo_insert('jy_qiuyuyue_order_goods',$data);
	}
	$this->session('carts','');	
    $params['fee']     = $price;
    $params['title']   = $order_type.'订单';
	$params['id']=$order_id;
	$params['plugin']='jy_qiuyuyue';
	$params['no_type']=array('hdfk');//可选，此项作用为指定不允许使用哪种支付方式
	$params['back_url']='jy_qiuyuyue/order/payresult';
	$pay_id=cfc('pay')->create_pay_order($params);
	$this->pdo_update('order',array('pay_id'=>$pay_id),array('id'=>$order_id));
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_qiuyuyue_order',array('id'=>$payresult['id']));
	if(!$order){
		exi('指定订单不存在！','error');
	}
	if($order['is_pay'] ==1){
		exi('该订单已经支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('订单金额不匹配！','error');
	}
	pdo_update('jy_qiuyuyue_order',array('pay_type'=>$payresult['pay_type'],'is_pay'=>1,'pay_id'=>$payresult['pay_id']),array('id'=>$payresult['id']));	
	if($this->plugin_exists('jy_commission')){
		//分销计入
		$order['plugin_name']='jy_qiuyuyue';
		model('jy_commission/commission')->create($order);
	}
	exi('支付成功!');
}

function array_order($value, $array)
{
    $array[] = $value;
    asort($array);
    $array = array_values($array);
    $index = array_search($value, $array);
    return $array[$index + 1];
}
?>